"""
Case Type   : cm_ctl可靠性
Case Name   : cm_ctl stop -n过程中删除文件cluster_manual_start
Create At   : 2022-08-11
Owner       : opentestcase012
Description :
    1: 执行cm_ctl stop -n命令,过程中修改文件cluster_manual_start不可读
    2: 查看集群状态
    3: 再次执行cm_ctl stop -n命令;
    4: 查看集群状态
    5: 启动节点;
Expect      :
    1: 修改成功,stop成功
    2: 集群状态异常，备节点停止
    3: 成功
    4: 集群状态异常，备节点停止
    5: 成功
History     :
"""

import os
import time
import unittest

from testcase.utils.ComThread import ComThread
from testcase.utils.Common import Common
from testcase.utils.CommonSH import CommonSH
from testcase.utils.Constant import Constant
from testcase.utils.Logger import Logger
from yat.test import Node
from yat.test import macro

Primary_SH = CommonSH('PrimaryDbUser')


# @unittest.skipIf(3 != Primary_SH.get_node_num(), '需1主2备环境，否则不执行')
class DefinedResource(unittest.TestCase):
    def setUp(self):
        self.log = Logger()
        self.log.info(f'-----{os.path.basename(__file__)} start-----')
        self.pri_user = Node('PrimaryDbUser')
        self.pri_root = Node('PrimaryRoot')
        self.sta1_user = Node('Standby1DbUser')
        self.constant = Constant()
        self.common = Common()
        self.start_file = os.path.join(macro.GAUSSHOME_BIN_PATH,
                                       'cluster_manual_start')

    def test_cm(self):
        text = '----step1:执行cm_ctl stop -n命令,过程中删除文件cluster_manual_start;' \
               ' expect:注入成功,stop成功----'
        self.log.info(text)
        stop_cmd = f'source {macro.DB_ENV_PATH};' \
                   f'sleep 5;cm_ctl stop -n 2;'
        self.log.info(stop_cmd)
        stop_thread = ComThread(self.pri_user.sh, args=(stop_cmd,))
        stop_thread.setDaemon(True)
        stop_thread.start()

        for num in range(300):
            rm_cmd = f'rm -rf {self.start_file};'
            rm_thread = ComThread(self.sta1_user.sh, args=(rm_cmd,))
            rm_thread.setDaemon(True)
            rm_thread.start()
            rm_thread.join(60)
            rm_res = rm_thread.get_result().result()
            if 'No such file' not in rm_res:
                self.log.info(rm_cmd)
                self.log.info(rm_res)
                self.assertEqual('', rm_res, "执行失败:" + text)
                break

        self.log.info('----获取stop结果----')
        stop_thread.join(120)
        result = stop_thread.get_result().result()
        self.log.info(result)
        self.assertIn(self.constant.cm_stop_node_success_msg, result,
                      "执行失败:" + text)

        text = '----step3:查看集群状态; expect:集群状态异常，备节点停止----'
        self.log.info(text)
        time.sleep(5)
        show_status = f'source {macro.DB_ENV_PATH};' \
            f'cm_ctl query -Civ'
        self.log.info(show_status)
        status_msg = self.common.get_sh_result(self.pri_user, show_status)
        self.assertIn(self.constant.CLUSTER_DEGRADED_MSG, status_msg,
                      "执行失败:" + text)
        self.assertIn('6002     S Down    Unknown', status_msg,
                      "执行失败:" + text)

        text = '----step4:再次执行cm_ctl stop -n命令; expect:执行停止----'
        self.log.info(text)
        self.log.info(stop_cmd)
        stop_msg = self.common.get_sh_result(self.pri_user, stop_cmd)
        self.assertIn(self.constant.cm_stop_node_success_msg, stop_msg,
                      "执行失败:" + text)

        text = '----step5:查看集群状态; expect:集群状态异常，备节点停止----'
        self.log.info(text)
        time.sleep(5)
        self.log.info(show_status)
        status_msg = self.common.get_sh_result(self.pri_user, show_status)
        self.assertIn(self.constant.CLUSTER_DEGRADED_MSG, status_msg,
                      "执行失败:" + text)
        self.assertIn('6002     S Down    Unknown', status_msg,
                      "执行失败:" + text)

    def tearDown(self):
        self.log.info('-----清理环境-----')
        text = '-----step6:启动节点; expect:成功-----'
        self.log.info(text)
        start_cmd = f'source {macro.DB_ENV_PATH};' \
            f'cm_ctl start -n 2;'
        self.log.info(start_cmd)
        start_msg = self.common.get_sh_result(self.pri_user, start_cmd)
        self.assertIn(self.constant.cm_start_node_success_msg, start_msg,
                      "执行失败:" + text)
        self.log.info(f'-----{os.path.basename(__file__)} end-----')
